package com.carey.blog.action.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.action.util.Filler;
import com.carey.blog.framework.latke.Keys;
import com.carey.blog.framework.latke.action.AbstractCacheablePageAction;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.service.LangPropsService;
import com.carey.blog.framework.latke.util.Locales;
import com.carey.blog.framework.latke.util.Strings;
import com.carey.blog.model.Page;
import com.carey.blog.model.Preference;
import com.carey.blog.repository.PageRepository;
import com.carey.blog.repository.impl.PageGAERepository;
import com.carey.blog.util.Pages;
import com.carey.blog.util.Preferences;

/**
 * Page action. page.ftl.
 * 
 */
public final class PageAction extends AbstractCacheablePageAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(PageAction.class
			.getName());
	/**
	 * Language service.
	 */
	private LangPropsService langPropsService = LangPropsService.getInstance();
	/**
	 * Filler.
	 */
	private Filler filler = Filler.getInstance();
	/**
	 * Page repository.
	 */
	private PageRepository pageRepository = PageGAERepository.getInstance();
	/**
	 * Page utilities.
	 */
	private Pages pageUtils = Pages.getInstance();
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final Map<String, Object> ret = new HashMap<String, Object>();

		try {
			final JSONObject queryStringJSONObject = getQueryStringJSONObject(request);

			final JSONObject preference = preferenceUtils.getPreference();
			if (null == preference) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return ret;
			}

			final String localeString = preference
					.getString(Preference.LOCALE_STRING);
			final Locale locale = new Locale(Locales.getLanguage(localeString),
					Locales.getCountry(localeString));

			final Map<String, String> langs = langPropsService.getAll(locale);
			ret.putAll(langs);

			String pageId = queryStringJSONObject.optString(Keys.OBJECT_ID);
			if (Strings.isEmptyOrNull(pageId)) {
				pageId = (String) request.getAttribute(Keys.OBJECT_ID);
			}

			if (Strings.isEmptyOrNull(pageId)) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);

				return ret;
			}

			final JSONObject page = pageRepository.get(pageId);
			ret.put(Page.PAGE, page);
			final List<JSONObject> comments = pageUtils.getComments(pageId);
			ret.put(Page.PAGE_COMMENTS_REF, comments);

			filler.fillSide(ret, preference);
			filler.fillBlogHeader(ret, preference);
			filler.fillBlogFooter(ret, preference);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);

			try {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);

				return ret;
			} catch (final IOException ex) {
				LOGGER.severe(ex.getMessage());
			}
		}

		return ret;
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
